﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Catering_Model;
using System.Web.Services;


namespace Catering.Catering_Page.Mes
{
    public partial class MobileOutBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Catering_Rules.SendedOutBoxInfo smsRules = new Catering_Rules.SendedOutBoxInfo();
            if (!string.IsNullOrEmpty(Request.QueryString["Deleteid"]))
            {
                smsRules.DeleteSendedOutBoxInfo(Convert.ToInt32(Request.QueryString["Deleteid"]));
            }
            if (!IsPostBack)
            {
                DataTable dt = smsRules.GetSendedOutBoxInfoList(" ORDER BY SendedOutBox.ID DESC ");
                if (dt != null && dt.Rows.Count > 0)
                {
                    RepeaterDataBind(dt, NoticeRept);
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageType"]) && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                SendedOutBoxInfo info = smsRules.GetSendedOutBoxInfoModel(Convert.ToInt32(Request.QueryString["id"]));
                SetDataToDivControls(info);
            }
        }

        private void RepeaterDataBind(DataTable dt, Repeater rpt)
        {
            DataColumn dc = new DataColumn("tempid");
            dt.Columns.Add(dc);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["tempid"] = i + 1;
            }
            PagedDataSource objPage = new PagedDataSource();
            //设置数据源 DefaultView：获取可能包含筛选视图或游标位置表的自定义视图
            objPage.DataSource = dt.DefaultView;
            //允许分页
            objPage.AllowPaging = true;
            //设置每页个数
            objPage.PageSize = 12;
            //定义变量保存当前页的索引值

            int CurPage;
            //判断是否具有页面跳转的请求
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            //设置当前页的索引
            objPage.CurrentPageIndex = CurPage - 1;
            //显示状态信息
            lblCurPage.Text = "第" + CurPage.ToString() + "/" + objPage.PageCount + "页";
            //如果当前页面不是首页
            if (!objPage.IsFirstPage)
            {
                //定义“上一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
                InkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

            }
            InkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (objPage.PageCount);
            InkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            //如果当前页面不是最后一页
            if (!objPage.IsLastPage)
            {
                //定义“下一页”超级链接的URL为：当前执行页面的虚拟路径，并传递下一页面的索引值 
                InkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            }
            //将PagedDataSource作为数据源
            rpt.DataSource = objPage;

            //进行数据绑定
            rpt.DataBind();
        }

        public string subStr(string str)
        {
            return (str.Length > 20)?str.Substring(0, 19) + ".......":str;
        }

        private void SetDataToDivControls(SendedOutBoxInfo dr)
        {
            //lblSender.Text = dr["uName"].ToString();
            lblSendTime.Text = dr.SendTime.ToString();
            td_content.InnerHtml = dr.Msg.ToString();
            divbg.Style[HtmlTextWriterStyle.Display] = "block";
            divmain.Style[HtmlTextWriterStyle.Display] = "block";
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            divbg.Style[HtmlTextWriterStyle.Display] = "none";
            divmain.Style[HtmlTextWriterStyle.Display] = "none";
        }

        [WebMethod]
        public static string Del_More(string IDs)
        {
            IDs = IDs.TrimEnd(',');
            if (!string.IsNullOrEmpty(IDs))
            {
                bool result = new Catering_Rules.SendedOutBoxInfo().DeleteSendedOutBoxInfoList(" AND ID IN(" + IDs + ")");
                if (result) return "OK";
            }
            return "NO";
        }
    }
}